
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django 异常 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="文件处理" href="files/index.html" />
    <link rel="prev" title="django-admin 和 manage.py" href="django-admin.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="django-admin.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django-admin&lt;/span&gt;&lt;/code&gt; 和 &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;manage.py&lt;/span&gt;&lt;/code&gt;">previous</a>
     |
    <a href="index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="files/index.html" title="文件处理">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-exceptions">
            
  <div class="section" id="s-django-exceptions">
<span id="django-exceptions"></span><h1>Django 异常<a class="headerlink" href="#django-exceptions" title="永久链接至标题">¶</a></h1>
<p>Django 会引发一些自己的异常以及标准的 Python 异常。</p>
<div class="section" id="s-module-django.core.exceptions">
<span id="s-django-core-exceptions"></span><span id="module-django.core.exceptions"></span><span id="django-core-exceptions"></span><h2>Django 核心异常<a class="headerlink" href="#module-django.core.exceptions" title="永久链接至标题">¶</a></h2>
<p>Django 核心异常类定义在 <code class="docutils literal notranslate"><span class="pre">django.core.exceptions</span></code> 中。</p>
<div class="section" id="s-appregistrynotready">
<span id="appregistrynotready"></span><h3><code class="docutils literal notranslate"><span class="pre">AppRegistryNotReady</span></code><a class="headerlink" href="#appregistrynotready" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.AppRegistryNotReady">
<em class="property">exception </em><code class="descname">AppRegistryNotReady</code><a class="reference internal" href="../_modules/django/core/exceptions.html#AppRegistryNotReady"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.AppRegistryNotReady" title="永久链接至目标">¶</a></dt>
<dd><p>当在初始化 ORM 的 <a class="reference internal" href="applications.html#app-loading-process"><span class="std std-ref">应用加载过程</span></a> 完成之前，试图使用模型时，就会引发这个异常。</p>
</dd></dl>

</div>
<div class="section" id="s-objectdoesnotexist">
<span id="objectdoesnotexist"></span><h3><code class="docutils literal notranslate"><span class="pre">ObjectDoesNotExist</span></code><a class="headerlink" href="#objectdoesnotexist" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.ObjectDoesNotExist">
<em class="property">exception </em><code class="descname">ObjectDoesNotExist</code><a class="reference internal" href="../_modules/django/core/exceptions.html#ObjectDoesNotExist"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.ObjectDoesNotExist" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="models/class.html#django.db.models.Model.DoesNotExist" title="django.db.models.Model.DoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Model.DoesNotExist</span></code></a> 异常的基类。一个对 <code class="docutils literal notranslate"><span class="pre">ObjectDoesNotExist</span></code> 的 <code class="docutils literal notranslate"><span class="pre">try/except</span></code> 将捕获对所有模型的 <a class="reference internal" href="models/class.html#django.db.models.Model.DoesNotExist" title="django.db.models.Model.DoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DoesNotExist</span></code></a> 异常。</p>
<p>参见 <a class="reference internal" href="models/querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-emptyresultset">
<span id="emptyresultset"></span><h3><code class="docutils literal notranslate"><span class="pre">EmptyResultSet</span></code><a class="headerlink" href="#emptyresultset" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.EmptyResultSet">
<em class="property">exception </em><code class="descname">EmptyResultSet</code><a class="reference internal" href="../_modules/django/core/exceptions.html#EmptyResultSet"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.EmptyResultSet" title="永久链接至目标">¶</a></dt>
<dd><p>如果一个查询不返回任何结果，那么 <code class="docutils literal notranslate"><span class="pre">EmptyResultSet</span></code> 可能会在生成查询的过程中被触发。大多数 Django 项目不会遇到这个异常，但它可能对实现自定义查找和表达式很有用。</p>
</dd></dl>

</div>
<div class="section" id="s-fielddoesnotexist">
<span id="fielddoesnotexist"></span><h3><code class="docutils literal notranslate"><span class="pre">FieldDoesNotExist</span></code><a class="headerlink" href="#fielddoesnotexist" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.FieldDoesNotExist">
<em class="property">exception </em><code class="descname">FieldDoesNotExist</code><a class="reference internal" href="../_modules/django/core/exceptions.html#FieldDoesNotExist"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.FieldDoesNotExist" title="永久链接至目标">¶</a></dt>
<dd><p>当请求的字段在模型或模型的父类上不存在时，模型的 <code class="docutils literal notranslate"><span class="pre">_meta.get_field()</span></code> 方法会引发 <code class="docutils literal notranslate"><span class="pre">FieldDoesNotExist</span></code> 异常。</p>
</dd></dl>

</div>
<div class="section" id="s-multipleobjectsreturned">
<span id="multipleobjectsreturned"></span><h3><code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code><a class="headerlink" href="#multipleobjectsreturned" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.MultipleObjectsReturned">
<em class="property">exception </em><code class="descname">MultipleObjectsReturned</code><a class="reference internal" href="../_modules/django/core/exceptions.html#MultipleObjectsReturned"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.MultipleObjectsReturned" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="models/class.html#django.db.models.Model.MultipleObjectsReturned" title="django.db.models.Model.MultipleObjectsReturned"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Model.MultipleObjectsReturned</span></code></a> 异常的基类。一个对 <code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code> 的 <code class="docutils literal notranslate"><span class="pre">try/except</span></code> 将捕获对所有模型的 <a class="reference internal" href="models/class.html#django.db.models.Model.MultipleObjectsReturned" title="django.db.models.Model.MultipleObjectsReturned"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code></a> 异常。</p>
<p>参见 <a class="reference internal" href="models/querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-suspiciousoperation">
<span id="suspiciousoperation"></span><h3><code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code><a class="headerlink" href="#suspiciousoperation" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.SuspiciousOperation">
<em class="property">exception </em><code class="descname">SuspiciousOperation</code><a class="reference internal" href="../_modules/django/core/exceptions.html#SuspiciousOperation"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.SuspiciousOperation" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a> 异常是在用户进行了从安全角度看应该被认为是可疑的操作时产生的，例如篡改会话 cookie。<code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code> 的子类包括：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">DisallowedHost</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">DisallowedModelAdminLookup</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">DisallowedModelAdminToField</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">DisallowedRedirect</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">InvalidSessionKey</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">RequestDataTooBig</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">SuspiciousFileOperation</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">SuspiciousMultipartForm</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">SuspiciousSession</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">TooManyFieldsSent</span></code></li>
</ul>
<p>如果一个 <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code> 异常达到 ASGI／WSGI 处理程序级别，它将被记录在 <code class="docutils literal notranslate"><span class="pre">Error</span></code> 级别，并导致一个 <a class="reference internal" href="request-response.html#django.http.HttpResponseBadRequest" title="django.http.HttpResponseBadRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseBadRequest</span></code></a>。更多信息请参见 :doc:<a href="#id1"><span class="problematic" id="id2">`</span></a>日志文档 <a href="#id3"><span class="problematic" id="id4">`</span></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-permissiondenied">
<span id="permissiondenied"></span><h3><code class="docutils literal notranslate"><span class="pre">PermissionDenied</span></code><a class="headerlink" href="#permissiondenied" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.PermissionDenied">
<em class="property">exception </em><code class="descname">PermissionDenied</code><a class="reference internal" href="../_modules/django/core/exceptions.html#PermissionDenied"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.PermissionDenied" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.core.exceptions.PermissionDenied" title="django.core.exceptions.PermissionDenied"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionDenied</span></code></a> 异常是在用户没有权限执行所请求的操作时发生的。</p>
</dd></dl>

</div>
<div class="section" id="s-viewdoesnotexist">
<span id="viewdoesnotexist"></span><h3><code class="docutils literal notranslate"><span class="pre">ViewDoesNotExist</span></code><a class="headerlink" href="#viewdoesnotexist" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.ViewDoesNotExist">
<em class="property">exception </em><code class="descname">ViewDoesNotExist</code><a class="reference internal" href="../_modules/django/core/exceptions.html#ViewDoesNotExist"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.ViewDoesNotExist" title="永久链接至目标">¶</a></dt>
<dd><p>当请求的视图不存在时， <a class="reference internal" href="#django.core.exceptions.ViewDoesNotExist" title="django.core.exceptions.ViewDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ViewDoesNotExist</span></code></a> 异常由 <a class="reference internal" href="urlresolvers.html#module-django.urls" title="django.urls"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.urls</span></code></a> 引发。</p>
</dd></dl>

</div>
<div class="section" id="s-middlewarenotused">
<span id="middlewarenotused"></span><h3><code class="docutils literal notranslate"><span class="pre">MiddlewareNotUsed</span></code><a class="headerlink" href="#middlewarenotused" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.MiddlewareNotUsed">
<em class="property">exception </em><code class="descname">MiddlewareNotUsed</code><a class="reference internal" href="../_modules/django/core/exceptions.html#MiddlewareNotUsed"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.MiddlewareNotUsed" title="永久链接至目标">¶</a></dt>
<dd><p>当服务器配置中没有使用中间件时，会引发 <a class="reference internal" href="#django.core.exceptions.MiddlewareNotUsed" title="django.core.exceptions.MiddlewareNotUsed"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MiddlewareNotUsed</span></code></a> 异常。</p>
</dd></dl>

</div>
<div class="section" id="s-improperlyconfigured">
<span id="improperlyconfigured"></span><h3><code class="docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code><a class="headerlink" href="#improperlyconfigured" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.ImproperlyConfigured">
<em class="property">exception </em><code class="descname">ImproperlyConfigured</code><a class="reference internal" href="../_modules/django/core/exceptions.html#ImproperlyConfigured"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.ImproperlyConfigured" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.core.exceptions.ImproperlyConfigured" title="django.core.exceptions.ImproperlyConfigured"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code></a> 异常是在 Django 配置不当的情况下产生的，例如，<code class="docutils literal notranslate"><span class="pre">settings.py</span></code> 中的一个值不正确或无法解析。</p>
</dd></dl>

</div>
<div class="section" id="s-fielderror">
<span id="fielderror"></span><h3><code class="docutils literal notranslate"><span class="pre">FieldError</span></code><a class="headerlink" href="#fielderror" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.FieldError">
<em class="property">exception </em><code class="descname">FieldError</code><a class="reference internal" href="../_modules/django/core/exceptions.html#FieldError"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.FieldError" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.core.exceptions.FieldError" title="django.core.exceptions.FieldError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FieldError</span></code></a> 异常是在模型字段出现问题时引发的。发生这种情况有几种原因：</p>
<ul class="simple">
<li>模型中的一个字段与抽象基类中的同名字段发生冲突</li>
<li>无限循环是由排序引起的</li>
<li>不能从过滤器参数中解析关键字</li>
<li>一个字段不能通过查询参数中的关键字来确定</li>
<li>不允许在指定字段上进行 join</li>
<li>字段名无效</li>
<li>查询包含无效的 order_by 参数</li>
</ul>
</dd></dl>

</div>
<div class="section" id="s-validationerror">
<span id="validationerror"></span><h3><code class="docutils literal notranslate"><span class="pre">ValidationError</span></code><a class="headerlink" href="#validationerror" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.ValidationError">
<em class="property">exception </em><code class="descname">ValidationError</code><a class="reference internal" href="../_modules/django/core/exceptions.html#ValidationError"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.ValidationError" title="永久链接至目标">¶</a></dt>
<dd><p>当数据未能通过表单或模型字段验证时，会引发 <a class="reference internal" href="#django.core.exceptions.ValidationError" title="django.core.exceptions.ValidationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValidationError</span></code></a> 异常。关于验证的更多信息，请参见 <a class="reference internal" href="forms/validation.html"><span class="doc">表单和字段验证</span></a>、 <a class="reference internal" href="models/instances.html#validating-objects"><span class="std std-ref">模型字段验证</span></a> 和 <a class="reference internal" href="validators.html"><span class="doc">验证器参考</span></a>。</p>
</dd></dl>

<div class="section" id="s-non-field-errors">
<span id="non-field-errors"></span><h4><code class="docutils literal notranslate"><span class="pre">NON_FIELD_ERRORS</span></code><a class="headerlink" href="#non-field-errors" title="永久链接至标题">¶</a></h4>
<dl class="data">
<dt id="django.core.exceptions.NON_FIELD_ERRORS">
<code class="descname">NON_FIELD_ERRORS</code><a class="headerlink" href="#django.core.exceptions.NON_FIELD_ERRORS" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">ValidationError</span></code> 不属于表格或模型中某一特定字段的错误被归类为 <code class="docutils literal notranslate"><span class="pre">NON_FIELD_ERRORS</span></code>。这个常量在字典中被用作键，否则就会将字段映射到它们各自的错误列表中。</p>
</div>
</div>
<div class="section" id="s-badrequest">
<span id="badrequest"></span><h3><code class="docutils literal notranslate"><span class="pre">BadRequest</span></code><a class="headerlink" href="#badrequest" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.BadRequest">
<em class="property">exception </em><code class="descname">BadRequest</code><a class="reference internal" href="../_modules/django/core/exceptions.html#BadRequest"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.BadRequest" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p><a class="reference internal" href="#django.core.exceptions.BadRequest" title="django.core.exceptions.BadRequest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadRequest</span></code></a> 异常是在请求由于客户端错误而不能被处理时引发的。如果一个 <code class="docutils literal notranslate"><span class="pre">BadRequest</span></code> 异常到达 ASGI／WSGI 处理程序级别，它会导致一个 <a class="reference internal" href="request-response.html#django.http.HttpResponseBadRequest" title="django.http.HttpResponseBadRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseBadRequest</span></code></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-requestaborted">
<span id="requestaborted"></span><h3><code class="docutils literal notranslate"><span class="pre">RequestAborted</span></code><a class="headerlink" href="#requestaborted" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.RequestAborted">
<em class="property">exception </em><code class="descname">RequestAborted</code><a class="reference internal" href="../_modules/django/core/exceptions.html#RequestAborted"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.RequestAborted" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.core.exceptions.RequestAborted" title="django.core.exceptions.RequestAborted"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RequestAborted</span></code></a> 异常是在处理程序读入的 HTTP body 在传输流中被切断，客户端连接关闭，或者客户端不发送数据，遇到超时，服务器关闭连接时引发的。</p>
<p>它是 HTTP 处理模块内部的，你不太可能在其他地方看到它。如果你正在修改 HTTP 处理代码，你应该在遇到一个中止的请求时提出这个问题，以确保 socket 被干净地关闭。</p>
</dd></dl>

</div>
<div class="section" id="s-synchronousonlyoperation">
<span id="synchronousonlyoperation"></span><h3><code class="docutils literal notranslate"><span class="pre">SynchronousOnlyOperation</span></code><a class="headerlink" href="#synchronousonlyoperation" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.core.exceptions.SynchronousOnlyOperation">
<em class="property">exception </em><code class="descname">SynchronousOnlyOperation</code><a class="reference internal" href="../_modules/django/core/exceptions.html#SynchronousOnlyOperation"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.core.exceptions.SynchronousOnlyOperation" title="永久链接至目标">¶</a></dt>
<dd><p>当只有在同步 Python 代码中才允许的代码被从异步上下文（一个有正在运行的异步事件循环的线程）中调用时，会引发 <a class="reference internal" href="#django.core.exceptions.SynchronousOnlyOperation" title="django.core.exceptions.SynchronousOnlyOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SynchronousOnlyOperation</span></code></a> 异常。Django 的这些部分一般都严重依赖线程安全来运行，在协程共享同一个线程的情况下无法正常工作。</p>
<p>如果你想从一个异步线程中调用只用于同步的代码，那么就创建一个同步线程并在该线程中调用它。你可以通过 <a class="reference internal" href="../topics/async.html#asgiref.sync.sync_to_async" title="asgiref.sync.sync_to_async"><code class="xref py py-func docutils literal notranslate"><span class="pre">asgiref.sync.sync_to_async()</span></code></a> 来实现。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-url-resolver-exceptions">
<span id="url-resolver-exceptions"></span><h2>URL 解析器异常<a class="headerlink" href="#url-resolver-exceptions" title="永久链接至标题">¶</a></h2>
<p>URL 解析器异常定义在 <code class="docutils literal notranslate"><span class="pre">django.urls</span></code> 中。</p>
<div class="section" id="s-resolver404">
<span id="resolver404"></span><h3><code class="docutils literal notranslate"><span class="pre">Resolver404</span></code><a class="headerlink" href="#resolver404" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.urls.Resolver404">
<em class="property">exception </em><code class="descname">Resolver404</code><a class="headerlink" href="#django.urls.Resolver404" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="urlresolvers.html#django.urls.resolve" title="django.urls.resolve"><code class="xref py py-exc docutils literal notranslate"><span class="pre">resolve()</span></code></a> 如果传递给 <code class="docutils literal notranslate"><span class="pre">resolve()</span></code> 的路径没有映射到视图，那么 <a class="reference internal" href="#django.urls.Resolver404" title="django.urls.Resolver404"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Resolver404</span></code></a> 异常就会被 <a class="reference internal" href="urlresolvers.html#django.urls.resolve" title="django.urls.resolve"><code class="xref py py-func docutils literal notranslate"><span class="pre">resolve()</span></code></a> 引发。这是 <a class="reference internal" href="../topics/http/views.html#django.http.Http404" title="django.http.Http404"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.http.Http404</span></code></a> 的一个子类。</p>
</dd></dl>

</div>
<div class="section" id="s-noreversematch">
<span id="noreversematch"></span><h3><code class="docutils literal notranslate"><span class="pre">NoReverseMatch</span></code><a class="headerlink" href="#noreversematch" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.urls.NoReverseMatch">
<em class="property">exception </em><code class="descname">NoReverseMatch</code><a class="headerlink" href="#django.urls.NoReverseMatch" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.urls.NoReverseMatch" title="django.urls.NoReverseMatch"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NoReverseMatch</span></code></a> 异常是由 <a class="reference internal" href="urlresolvers.html#module-django.urls" title="django.urls"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.urls</span></code></a> 引发的，当你的 URLconf 中不能根据提供的参数识别出一个匹配的 URL 时会引发。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-database-exceptions">
<span id="database-exceptions"></span><h2>数据库异常<a class="headerlink" href="#database-exceptions" title="永久链接至标题">¶</a></h2>
<p>数据库异常可以从 <code class="docutils literal notranslate"><span class="pre">django.db</span></code> 导入。</p>
<p>Django 封装了标准的数据库异常，这样你的 Django 代码就可以保证这些类的通用实现。</p>
<dl class="exception">
<dt id="django.db.Error">
<em class="property">exception </em><code class="descname">Error</code><a class="headerlink" href="#django.db.Error" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.InterfaceError">
<em class="property">exception </em><code class="descname">InterfaceError</code><a class="headerlink" href="#django.db.InterfaceError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.DatabaseError">
<em class="property">exception </em><code class="descname">DatabaseError</code><a class="headerlink" href="#django.db.DatabaseError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.DataError">
<em class="property">exception </em><code class="descname">DataError</code><a class="headerlink" href="#django.db.DataError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.OperationalError">
<em class="property">exception </em><code class="descname">OperationalError</code><a class="headerlink" href="#django.db.OperationalError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.IntegrityError">
<em class="property">exception </em><code class="descname">IntegrityError</code><a class="headerlink" href="#django.db.IntegrityError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.InternalError">
<em class="property">exception </em><code class="descname">InternalError</code><a class="headerlink" href="#django.db.InternalError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.ProgrammingError">
<em class="property">exception </em><code class="descname">ProgrammingError</code><a class="headerlink" href="#django.db.ProgrammingError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="exception">
<dt id="django.db.NotSupportedError">
<em class="property">exception </em><code class="descname">NotSupportedError</code><a class="headerlink" href="#django.db.NotSupportedError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>数据库异常的 Django 包装器的行为与底层数据库异常完全相同。更多信息请参见 <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0249"><strong>PEP 249</strong></a>，Python 数据库 API 规范 v2.0。</p>
<p>根据 <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>，<code class="docutils literal notranslate"><span class="pre">__cause__</span></code> 属性与原始（基础）数据库异常一起设置，允许访问提供的任何附加信息。</p>
<dl class="exception">
<dt id="django.db.models.ProtectedError">
<em class="property">exception </em><code class="descclassname">models.</code><code class="descname">ProtectedError</code><a class="headerlink" href="#django.db.models.ProtectedError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>当使用 <a class="reference internal" href="models/fields.html#django.db.models.PROTECT" title="django.db.models.PROTECT"><code class="xref py py-attr docutils literal notranslate"><span class="pre">django.db.models.PROTECT</span></code></a> 时，为了防止删除引用对象而引发的 <a class="reference internal" href="#django.db.models.ProtectedError" title="django.db.models.ProtectedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">models.ProtectedError</span></code></a> 是 <a class="reference internal" href="#django.db.IntegrityError" title="django.db.IntegrityError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IntegrityError</span></code></a> 的子类。</p>
<dl class="exception">
<dt id="django.db.models.RestrictedError">
<em class="property">exception </em><code class="descclassname">models.</code><code class="descname">RestrictedError</code><a class="headerlink" href="#django.db.models.RestrictedError" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>当使用 <a class="reference internal" href="models/fields.html#django.db.models.RESTRICT" title="django.db.models.RESTRICT"><code class="xref py py-attr docutils literal notranslate"><span class="pre">django.db.models.RESTRICT</span></code></a> 时，为了防止删除引用对象而引发的 <a class="reference internal" href="#django.db.models.RestrictedError" title="django.db.models.RestrictedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">models.RestrictedError</span></code></a> 是 <a class="reference internal" href="#django.db.IntegrityError" title="django.db.IntegrityError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IntegrityError</span></code></a> 的子类。</p>
</div>
<div class="section" id="s-http-exceptions">
<span id="http-exceptions"></span><h2>Http 异常<a class="headerlink" href="#http-exceptions" title="永久链接至标题">¶</a></h2>
<p>Http 异常可以从 <code class="docutils literal notranslate"><span class="pre">django.http</span></code> 导入。</p>
<div class="section" id="s-unreadableposterror">
<span id="unreadableposterror"></span><h3><code class="docutils literal notranslate"><span class="pre">UnreadablePostError</span></code><a class="headerlink" href="#unreadableposterror" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.http.UnreadablePostError">
<em class="property">exception </em><code class="descname">UnreadablePostError</code><a class="headerlink" href="#django.http.UnreadablePostError" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.http.UnreadablePostError" title="django.http.UnreadablePostError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnreadablePostError</span></code></a> 在用户取消上传时引发。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-sessions-exceptions">
<span id="sessions-exceptions"></span><h2>会话异常<a class="headerlink" href="#sessions-exceptions" title="永久链接至标题">¶</a></h2>
<p>会话异常在 <code class="docutils literal notranslate"><span class="pre">django.contrib.session.exceptions</span></code> 中定义。</p>
<div class="section" id="s-sessioninterrupted">
<span id="sessioninterrupted"></span><h3><code class="docutils literal notranslate"><span class="pre">SessionInterrupted</span></code><a class="headerlink" href="#sessioninterrupted" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.contrib.sessions.exceptions.SessionInterrupted">
<em class="property">exception </em><code class="descname">SessionInterrupted</code><a class="reference internal" href="../_modules/django/contrib/sessions/exceptions.html#SessionInterrupted"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#django.contrib.sessions.exceptions.SessionInterrupted" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p><a class="reference internal" href="#django.contrib.sessions.exceptions.SessionInterrupted" title="django.contrib.sessions.exceptions.SessionInterrupted"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SessionInterrupted</span></code></a> 是当一个会话在一个并发请求中被破坏时引发的。它是 <a class="reference internal" href="#django.core.exceptions.BadRequest" title="django.core.exceptions.BadRequest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadRequest</span></code></a> 的一个子类。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-transaction-exceptions">
<span id="transaction-exceptions"></span><h2>事务异常<a class="headerlink" href="#transaction-exceptions" title="永久链接至标题">¶</a></h2>
<p>事务异常在 <code class="docutils literal notranslate"><span class="pre">django.db.transaction</span></code> 中定义。</p>
<div class="section" id="s-transactionmanagementerror">
<span id="transactionmanagementerror"></span><h3><code class="docutils literal notranslate"><span class="pre">TransactionManagementError</span></code><a class="headerlink" href="#transactionmanagementerror" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.db.transaction.TransactionManagementError">
<em class="property">exception </em><code class="descname">TransactionManagementError</code><a class="headerlink" href="#django.db.transaction.TransactionManagementError" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.db.transaction.TransactionManagementError" title="django.db.transaction.TransactionManagementError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TransactionManagementError</span></code></a> 是针对与数据库事务有关的任何和所有问题提出的。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-testing-framework-exceptions">
<span id="testing-framework-exceptions"></span><h2>测试框架异常<a class="headerlink" href="#testing-framework-exceptions" title="永久链接至标题">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">django.test</span></code> 包提供的异常。</p>
<div class="section" id="s-redirectcycleerror">
<span id="redirectcycleerror"></span><h3><code class="docutils literal notranslate"><span class="pre">RedirectCycleError</span></code><a class="headerlink" href="#redirectcycleerror" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.test.client.RedirectCycleError">
<em class="property">exception </em><code class="descclassname">client.</code><code class="descname">RedirectCycleError</code><a class="headerlink" href="#django.test.client.RedirectCycleError" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.test.client.RedirectCycleError" title="django.test.client.RedirectCycleError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RedirectCycleError</span></code></a> 当测试客户端检测到一个循环或过长的重定向链时，就会引发。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-python-exceptions">
<span id="python-exceptions"></span><h2>Python 异常<a class="headerlink" href="#python-exceptions" title="永久链接至标题">¶</a></h2>
<p>Django 也会在适当的时候引发内置的 Python 异常。更多关于 <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#bltin-exceptions" title="(在 Python v3.10)"><span>Built-in Exceptions</span></a> 的信息请参见 Python 文档。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django 异常</a><ul>
<li><a class="reference internal" href="#module-django.core.exceptions">Django 核心异常</a><ul>
<li><a class="reference internal" href="#appregistrynotready"><code class="docutils literal notranslate"><span class="pre">AppRegistryNotReady</span></code></a></li>
<li><a class="reference internal" href="#objectdoesnotexist"><code class="docutils literal notranslate"><span class="pre">ObjectDoesNotExist</span></code></a></li>
<li><a class="reference internal" href="#emptyresultset"><code class="docutils literal notranslate"><span class="pre">EmptyResultSet</span></code></a></li>
<li><a class="reference internal" href="#fielddoesnotexist"><code class="docutils literal notranslate"><span class="pre">FieldDoesNotExist</span></code></a></li>
<li><a class="reference internal" href="#multipleobjectsreturned"><code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code></a></li>
<li><a class="reference internal" href="#suspiciousoperation"><code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a></li>
<li><a class="reference internal" href="#permissiondenied"><code class="docutils literal notranslate"><span class="pre">PermissionDenied</span></code></a></li>
<li><a class="reference internal" href="#viewdoesnotexist"><code class="docutils literal notranslate"><span class="pre">ViewDoesNotExist</span></code></a></li>
<li><a class="reference internal" href="#middlewarenotused"><code class="docutils literal notranslate"><span class="pre">MiddlewareNotUsed</span></code></a></li>
<li><a class="reference internal" href="#improperlyconfigured"><code class="docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code></a></li>
<li><a class="reference internal" href="#fielderror"><code class="docutils literal notranslate"><span class="pre">FieldError</span></code></a></li>
<li><a class="reference internal" href="#validationerror"><code class="docutils literal notranslate"><span class="pre">ValidationError</span></code></a><ul>
<li><a class="reference internal" href="#non-field-errors"><code class="docutils literal notranslate"><span class="pre">NON_FIELD_ERRORS</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#badrequest"><code class="docutils literal notranslate"><span class="pre">BadRequest</span></code></a></li>
<li><a class="reference internal" href="#requestaborted"><code class="docutils literal notranslate"><span class="pre">RequestAborted</span></code></a></li>
<li><a class="reference internal" href="#synchronousonlyoperation"><code class="docutils literal notranslate"><span class="pre">SynchronousOnlyOperation</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#url-resolver-exceptions">URL 解析器异常</a><ul>
<li><a class="reference internal" href="#resolver404"><code class="docutils literal notranslate"><span class="pre">Resolver404</span></code></a></li>
<li><a class="reference internal" href="#noreversematch"><code class="docutils literal notranslate"><span class="pre">NoReverseMatch</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#database-exceptions">数据库异常</a></li>
<li><a class="reference internal" href="#http-exceptions">Http 异常</a><ul>
<li><a class="reference internal" href="#unreadableposterror"><code class="docutils literal notranslate"><span class="pre">UnreadablePostError</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#sessions-exceptions">会话异常</a><ul>
<li><a class="reference internal" href="#sessioninterrupted"><code class="docutils literal notranslate"><span class="pre">SessionInterrupted</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#transaction-exceptions">事务异常</a><ul>
<li><a class="reference internal" href="#transactionmanagementerror"><code class="docutils literal notranslate"><span class="pre">TransactionManagementError</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#testing-framework-exceptions">测试框架异常</a><ul>
<li><a class="reference internal" href="#redirectcycleerror"><code class="docutils literal notranslate"><span class="pre">RedirectCycleError</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#python-exceptions">Python 异常</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="django-admin.html"
                        title="上一章"><code class="docutils literal notranslate"><span class="pre">django-admin</span></code> 和 <code class="docutils literal notranslate"><span class="pre">manage.py</span></code></a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="files/index.html"
                        title="下一章">文件处理</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/exceptions.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="django-admin.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django-admin&lt;/span&gt;&lt;/code&gt; 和 &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;manage.py&lt;/span&gt;&lt;/code&gt;">previous</a>
     |
    <a href="index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="files/index.html" title="文件处理">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>